import { createRouter, createWebHistory } from 'vue-router'

// 布局组件
const MainLayout = () => import('@/components/layout/MainLayout.vue')
const AuthLayout = () => import('@/components/layout/AuthLayout.vue')

// 路由懒加载
const Home = () => import('@/views/home/Index.vue')
const Login = () => import('@/views/auth/Login.vue')
const Register = () => import('@/views/auth/Register.vue')
const ForgotPassword = () => import('@/views/auth/ForgotPassword.vue')
const Personal = () => import('@/views/personal/Index.vue')
const Users = () => import('@/views/admin/Users.vue')
const AdminJobs = () => import('@/views/admin/AdminJobs.vue')

// 页面组件
const Jobs = () => import('@/views/jobs/Index.vue')
const JobFairs = () => import('@/views/job-fairs/Index.vue')
const Forum = () => import('@/views/forum/Index.vue')
const About = () => import('@/views/about/Index.vue')
const Contact = () => import('@/views/contact/Index.vue')
const JoinUs = () => import('@/views/join-us/Index.vue')
const CareerGuide = () => import('@/views/career-guide/Index.vue')
const ResumeTips = () => import('@/views/resume-tips/Index.vue')
const PostJob = () => import('@/views/employer/PostJob.vue')
const TalentSearch = () => import('@/views/employer/TalentSearch.vue')
const EmployerGuide = () => import('@/views/employer/Guide.vue')
const FAQ = () => import('@/views/help/FAQ.vue')
const Terms = () => import('@/views/help/Terms.vue')
const Privacy = () => import('@/views/help/Privacy.vue')

// 路由配置
const routes = [
  {
    path: '/',
    component: MainLayout,
    children: [
      {
        path: '',
        name: 'Home',
        component: Home,
        meta: { title: '首页 - 求职招聘网站' }
      },
      {
        path: 'jobs',
        name: 'Jobs',
        component: () => import('@/views/jobs/JobList.vue'),
        meta: { title: '职位列表 - 求职招聘网站' }
      },
      {
        path: 'jobs/:id',
        name: 'JobDetail',
        component: () => import('@/views/jobs/JobDetail.vue'),
        meta: { title: '职位详情 - 求职招聘网站' }
      },
      {
        path: 'job-fairs',
        name: 'JobFairs',
        component: () => import('@/views/job-fairs/JobFairList.vue'),
        meta: { title: '招聘会 - 求职招聘网站' }
      },
      {
        path: 'job-fairs/:id',
        name: 'JobFairDetail',
        component: () => import('@/views/job-fairs/JobFairDetail.vue'),
        meta: {
          title: '招聘会详情'
        }
      },
      {
        path: 'forum',
        name: 'Forum',
        component: Forum,
        meta: { title: '论坛 - 求职招聘网站' }
      },
      {
        path: 'forum/boards/:id',
        name: 'BoardDetail',
        component: () => import('@/views/forum/BoardDetail.vue'),
        meta: {
          title: '板块详情',
          requiresAuth: false
        }
      },
      {
        path: 'forum/topics/:id',
        name: 'TopicDetail',
        component: () => import('@/views/forum/TopicDetail.vue'),
        meta: {
          title: '话题详情',
          requiresAuth: false
        }
      },
      {
        path: 'forum/create',
        name: 'CreateTopic',
        component: () => import('@/views/forum/CreateTopic.vue'),
        meta: {
          title: '发表话题',
          requiresAuth: true
        }
      },
      {
        path: 'forum/edit/:id',
        name: 'EditTopic',
        component: () => import('@/views/forum/CreateTopic.vue'),
        meta: {
          title: '编辑话题',
          requiresAuth: true
        }
      },
      {
        path: 'forum/hot',
        name: 'HotTopics',
        component: () => import('@/views/forum/Index.vue'),
        meta: {
          title: '热门话题',
          requiresAuth: false,
          hotFilter: true
        }
      },
      {
        path: 'about',
        name: 'About',
        component: About,
        meta: { title: '关于我们 - 求职招聘网站' }
      },
      {
        path: 'contact',
        name: 'Contact',
        component: Contact,
        meta: { title: '联系我们 - 求职招聘网站' }
      },
      {
        path: 'join-us',
        name: 'JoinUs',
        component: JoinUs,
        meta: { title: '加入我们 - JobLinker' }
      },
      {
        path: 'career-guide',
        name: 'CareerGuide',
        component: CareerGuide,
        meta: { title: '求职指南 - 求职招聘网站' }
      },
      {
        path: 'resume-tips',
        name: 'ResumeTips',
        component: ResumeTips,
        meta: { title: '简历技巧 - 求职招聘网站' }
      },
      {
        path: 'post-job',
        name: 'PostJob',
        component: PostJob,
        meta: { 
          title: '发布职位 - 求职招聘网站',
          requiresAuth: true,
          roles: [1] // 招聘者角色
        }
      },
      {
        path: 'talent-search',
        name: 'TalentSearch',
        component: TalentSearch,
        meta: { 
          title: '人才搜索 - 求职招聘网站',
          requiresAuth: true,
          roles: [1] // 招聘者角色
        }
      },
      {
        path: 'employer-guide',
        name: 'EmployerGuide',
        component: EmployerGuide,
        meta: { title: '招聘指南 - 求职招聘网站' }
      },
      {
        path: 'faq',
        name: 'FAQ',
        component: FAQ,
        meta: { title: '常见问题 - 求职招聘网站' }
      },
      {
        path: 'terms',
        name: 'Terms',
        component: Terms,
        meta: { title: '服务条款 - 求职招聘网站' }
      },
      {
        path: 'privacy',
        name: 'Privacy',
        component: Privacy,
        meta: { title: '隐私政策 - 求职招聘网站' }
      },
      {
        path: 'personal',
        name: 'Personal',
        component: Personal,
        meta: { 
          title: '个人中心 - 求职招聘网站',
          requiresAuth: true
        }
      },
      {
        path: 'admin/users',
        name: 'AdminUsers',
        component: Users,
        meta: { 
          title: '用户管理 - 求职招聘网站',
          requiresAuth: true,
          roles: [2] // 管理员角色
        }
      },
      {
        path: 'admin/jobs',
        name: 'AdminJobs',
        component: AdminJobs,
        meta: { 
          title: '职位管理 - 求职招聘网站',
          requiresAuth: true,
          roles: [2] // 管理员角色
        }
      },
      {
        path: 'recruiter/dashboard',
        name: 'RecruiterDashboard',
        component: () => import('@/views/recruiter/Dashboard.vue'),
        meta: { 
          title: '招聘管理 - 求职招聘网站',
          requiresAuth: true,
          roles: [1] // 招聘者角色
        }
      },
      {
        path: 'recruiter/jobs',
        name: 'RecruiterJobs',
        component: () => import('@/views/recruiter/Jobs.vue'),
        meta: { 
          title: '职位管理 - 求职招聘网站',
          requiresAuth: true,
          roles: [1] // 招聘者角色
        }
      },
      {
        path: 'recruiter/resumes',
        name: 'RecruiterResumes',
        component: () => import('@/views/recruiter/Resumes.vue'),
        meta: { 
          title: '简历搜索 - 求职招聘网站',
          requiresAuth: true,
          roles: [1] // 招聘者角色
        }
      },
      {
        path: 'recruiter/applications',
        name: 'RecruiterApplications',
        component: () => import('@/views/recruiter/Applications.vue'),
        meta: { 
          title: '投递管理 - 求职招聘网站',
          requiresAuth: true,
          roles: [1] // 招聘者角色
        }
      },
      {
        path: 'admin/job-fairs',
        name: 'AdminJobFairs',
        component: () => import('@/views/admin/JobFairs.vue'),
        meta: {
          title: '招聘会管理',
          requiresAuth: true,
          roles: [2] // 仅管理员可访问
        }
      },
      {
        path: 'admin/job-fairs/:id/detail',
        name: 'AdminJobFairDetail',
        component: () => import('@/views/job-fairs/JobFairDetail.vue'),
        meta: {
          title: '招聘会详情',
          requiresAuth: true,
          roles: [2] // 仅管理员可访问
        }
      },
      {
        path: 'seeker/resumes',
        name: 'SeekerResumes',
        component: () => import('@/views/resumes/ResumeList.vue'),
        meta: { 
          title: '我的简历 - 求职招聘网站',
          requiresAuth: true,
          roles: [0] // 求职者角色
        }
      },
      {
        path: 'resumes/create',
        name: 'CreateResume',
        component: () => import('@/views/resumes/ResumeEdit.vue'),
        meta: {
          title: '创建简历',
          requiresAuth: true
        }
      },
      {
        path: 'resumes/:id/edit',
        name: 'EditResume',
        component: () => import('@/views/resumes/ResumeEdit.vue'),
        meta: { 
          title: '编辑简历 - 求职招聘网站',
          requiresAuth: true,
          roles: [0] // 求职者角色
        }
      },
      {
        path: 'resumes/:id/preview',
        name: 'PreviewResume',
        component: () => import('@/views/resumes/ResumePreview.vue'),
        meta: { 
          title: '预览简历 - 求职招聘网站',
          requiresAuth: true,
          roles: [0] // 求职者角色
        }
      },
      {
        path: 'seeker/applications',
        name: 'SeekerApplications',
        component: () => import('@/components/personal/JobApplications.vue'),
        meta: { 
          title: '投递记录 - 求职招聘网站',
          requiresAuth: true,
          roles: [0] // 求职者角色
        }
      },
      {
        path: 'seeker/favorites',
        name: 'SeekerFavorites',
        component: () => import('@/components/personal/FavoriteJobs.vue'),
        meta: { 
          title: '收藏职位 - 求职招聘网站',
          requiresAuth: true,
          roles: [0] // 求职者角色
        }
      },
      {
        path: 'companies/:id',
        name: 'CompanyDetail',
        component: () => import('@/views/companies/CompanyDetail.vue'),
        meta: { title: '公司详情 - 求职招聘网站' }
      },
      {
        path: 'resume/templates',
        name: 'ResumeTemplates',
        component: () => import('@/views/resume/TemplateSelect'),
        meta: { title: '选择简历模板', requireAuth: true }
      }
    ]
  },
  {
    path: '/auth',
    component: AuthLayout,
    children: [
      {
        path: 'login',
        name: 'Login',
        component: Login,
        meta: { title: '登录 - 求职招聘网站' }
      },
      {
        path: 'register',
        name: 'Register',
        component: Register,
        meta: { title: '注册 - 求职招聘网站' }
      },
      {
        path: 'forgot-password',
        name: 'ForgotPassword',
        component: ForgotPassword,
        meta: { title: '忘记密码 - 求职招聘网站' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }

  // 获取token和用户信息
  const token = localStorage.getItem('token')
  const userStr = localStorage.getItem('user')
  let userType = null
  
  if (token && userStr) {
    try {
      const user = JSON.parse(userStr)
      userType = user.role
    } catch (e) {
      console.warn('用户信息解析失败')
    }
  }

  // 需要登录的页面
  if (to.meta.requiresAuth) {
    if (!token) {
      next({ name: 'Login', query: { redirect: to.fullPath } })
      return
    }
    
    // 检查角色权限
    if (to.meta.roles && !to.meta.roles.includes(userType)) {
      next({ name: 'Home' })
      return
    }
  }

  // 已登录用户不能访问登录和注册页面
  if (token && (to.name === 'Login' || to.name === 'Register')) {
    next({ name: 'Home' })
    return
  }

  next()
})

export default router 